import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import Login from './pages/Login';
import MainLayout from './components/MainLayout';
import { userStorage } from './utils/storage';
import './App.css';

// 路由守卫组件
function PrivateRoute({ children }) {
  // 检查用户信息是否存在（使用 storage.js 会自动检查过期时间）
  const userInfo = userStorage.getUserInfo();
  const isLoggedIn = userInfo && userStorage.isLoggedIn();
  return isLoggedIn ? children : <Navigate to="/login" />;
}

function App() {
  return (
    <Routes>
      <Route path="/login" element={<Login />} />
      
      {/* 所有 /main 路由都使用 MainLayout */}
      <Route
        path="/main/*"
        element={
          <PrivateRoute>
            <MainLayout />
          </PrivateRoute>
        }
      />
      
      {/* 默认路由 */}
      <Route path="/" element={<Navigate to="/login" />} />
    </Routes>
  );
}

export default App;
